#!/usr/bin/env python3

import os
import zipfile

import common

class Main(common.LkmcCliFunction):
    def __init__(self):
        super().__init__(
            description='''\
https://github.com/cirosantilli/linux-kernel-module-cheat#release-zip
''',
            defaults = {
                'show_time': False,
            }
        )
        self.zip_files = []

    def timed_main(self):
        self.zip_files.append(self.env['qcow2_file'])
        self.zip_files.append(self.env['linux_image'])
        for root, dirnames, filenames in os.walk(self.env['baremetal_build_dir']):
            for filename in sorted(filenames):
                path = os.path.join(root, filename)
                if os.path.splitext(path)[1] == self.env['baremetal_executable_ext']:
                    self.zip_files.append(path)

    def teardown(self):
        os.makedirs(self.env['release_dir'], exist_ok=True)
        self.sh.rmrf(self.env['release_zip_file'])
        self.log_info('Creating zip: ' + self.env['release_zip_file'])
        with zipfile.ZipFile(self.env['release_zip_file'], 'w', zipfile.ZIP_DEFLATED) as zipf:
            for zip_file in self.zip_files:
                self.log_info('Adding file: ' + zip_file)
                zipf.write(zip_file, arcname=os.path.relpath(zip_file, self.env['root_dir']))

if __name__ == '__main__':
    Main().cli()
